package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BasePdaAddDto;
import com.kaili.basic.pojo.dto.BasePdaQueryDto;
import com.kaili.basic.pojo.dto.BasePdaVerifyDto;
import com.kaili.basic.pojo.entity.BaseCompany;
import com.kaili.basic.pojo.entity.BasePda;
import com.kaili.basic.service.BasePdaService;
import com.kaili.basic.service.IBaseCompanyService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.tenant.DbContextHolder;
import com.kaili.common.util.CommonConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "PDA管理", description = "PDA管理相关API")
public class BasePdaController {
    @Autowired
    private BasePdaService basePdaService;
    @Autowired
    IBaseCompanyService baseCompanyService;

    @PostMapping("/pdas")
    @ApiOperation(value = "新增PDA", notes = "新增PDA")
    public RestStatus addBasePda(@Validated @RequestBody BasePdaAddDto dto) {
        BasePda bp = basePdaService.getBySn(dto.getPdaSn());
        if (null == bp) {
            basePdaService.addBasePda(dto);
            return CommonStatus.ADD_OK;
        }else {
            return CommonStatus.ADD_DEFEAT;
        }
    }

    @DeleteMapping("/pdas/{id}")
    @ApiOperation(value = "删除PDA", notes = "删除PDA")
    public RestStatus deleteBasePda(@NotNull @PathVariable("id") @ApiParam(value = "PDA的ID") Long id) {
        basePdaService.deleteBasePda(id);
        return CommonStatus.DELETE_OK;
    }

    @PutMapping("/pdas")
    @ApiOperation(value = "更新PDA", notes = "更新PDA")
    public RestStatus updateBasePda(@Validated @RequestBody BasePdaAddDto dto) {
        if (dto != null) {
            BasePda bp = basePdaService.getBySn(dto.getPdaSn());
            if (bp!=null && !Objects.equals(bp.getPdaId(), dto.getPdaId())) {
                return CommonStatus.ADD_DEFEAT;
            }
            basePdaService.updateBasePda(dto);
            return CommonStatus.UPDATE_OK;
        }
        return CommonStatus.BAD_REQUEST;
    }

    @GetMapping("/pdas")
    @ApiOperation(value = "分页查询PDA", notes = "分页查询PDA")
    public Page<BasePda> queryBasePda(@ModelAttribute BasePdaQueryDto dto) {
        return basePdaService.queryBasePda(new Page<BasePda>(dto.getPage(), dto.getPageSize()), dto);
    }

    @GetMapping("/pdas/sns/{pdaSn}")
    @ApiOperation(value = "模糊查询SN码", notes = "模糊查询SN码")
    public List<String> selectLikeSn(@NotNull @PathVariable("pdaSn") @ApiParam(value = "PDA的SN") String pdaSn) {
        return basePdaService.selectBySn(pdaSn);
    }

    @GetMapping("/pdas/veriBySn")
    @ApiOperation(value = "验证SN码", notes = "验证SN码")
    public RestStatus veriBySn(@Validated @ModelAttribute BasePdaVerifyDto dto) {
        DbContextHolder.setDbType(CommonConstant.MAIN_DB_READ);
        BaseCompany baseCompany = baseCompanyService.getCompanyUrl(Long.parseLong(dto.getCompanyId()));
        if (baseCompany == null) {
            return CommonStatus.VALIDATE_FAILED;
        }
        if (baseCompany.getCompanyCode() == null) {
            return CommonStatus.VALIDATE_FAILED;
        }
        DbContextHolder.setDbType(dto.getCompanyId()+"_read");
        if(basePdaService.veriBySn(dto.getPdaSn()).size()>0)
            return CommonStatus.VALIDATE_OK;
        else
            return CommonStatus.VALIDATE_FAILED;
    }
}
